<!DOCTYPE html>
<script src="../../../../../resources/js-test.js"></script>
<style>
body {
    width: 2000px;
    height: 2000px;
}
</style>

<script>
var jsTestIsAsync = true;

description("This test verifies that smooth scrolls initiated on the main " +
            "thread add the appropriate main thread scrolling reason.");

// From ScrollingCoordinator::mainThreadScrollingReasonsAsText.
var ANIMATING_TEXT = "Handling scroll from main thread";
var IDLE = "Idle";
var RUNNING_ON_COMPOSITOR = "RunningOnCompositor";

function finishTest() {
  requestAnimationFrame(function() {
    shouldBe("document.scrollingElement.scrollTop", "40");
    // Check that main thread scrolling reason is removed.
    shouldBeTrue("internals.mainThreadScrollingReasons(document) == ''");
    finishJSTest();
  });
}

function runTest() {
    if (document.scrollingElement.scrollTop == 0) {
        requestAnimationFrame(runTest);
    } else {
        // Check that initiated by main thread and running on the compositor.
        shouldBe("internals.getScrollAnimationState(document)",
            "RUNNING_ON_COMPOSITOR");
        // Check that main thread scrolling reason is added.
        shouldBe("internals.mainThreadScrollingReasons(document)",
            "ANIMATING_TEXT");
        // Wait for scroll animation to complete.
        shouldBecomeEqual("internals.getScrollAnimationState(document)",
            "IDLE", finishTest);
    }
}

onload = function() {
    if (!window.eventSender || !window.internals) {
        debug("This test requires window.eventSender.")
        finishJSTest();
        return;
    }
    internals.settings.setScrollAnimatorEnabled(true);

    document.scrollingElement.scrollTop = 0;

    // Scroll 1 ticks down.
    eventSender.mouseMoveTo(20, 20);
    eventSender.mouseScrollBy(0, -1);
    runTest();
};

</script>
